package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.kitchen.domain.user.WxTemplate;
import org.dromara.kitchen.service.IWxTemplateService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 微信公众号的模板消息列
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/wx/template")
public class WxTemplateController extends BaseController {

    private final IWxTemplateService wxTemplateService;

    /**
     * 查询微信公众号的模板消息列列表
     */
    @SaCheckPermission("kitchen:template:list")
    @GetMapping("/list")
    public TableDataInfo<WxTemplate> list(WxTemplate wxTemplate, PageQuery pageQuery) {
        QueryWrapper<WxTemplate> qw = new QueryWrapper<>();

        TableDataInfo<WxTemplate> td = PageUtils.buildDataInfo(wxTemplateService.page(PageUtils.buildPage(),qw));
        List<WxTemplate> list = td.getRows();
        return td;
    }

    /**
     * 导出微信公众号的模板消息列列表
     */
    @SaCheckPermission("kitchen:template:export")
    @Log(title = "微信公众号的模板消息列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxTemplate wxTemplate, HttpServletResponse response) {
        List<WxTemplate> list = wxTemplateService.list();
        ExcelUtil.exportExcel(list, "微信公众号的模板消息列", WxTemplate.class, response);
    }

    /**
     * 获取微信公众号的模板消息列详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:template:query")
    @GetMapping("/{id}")
    public R<WxTemplate> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(wxTemplateService.getById(id));
    }

    /**
     * 新增微信公众号的模板消息列
     */
    @SaCheckPermission("kitchen:template:add")
    @Log(title = "微信公众号的模板消息列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxTemplate wxTemplate) {
        return toAjax(wxTemplateService.save(wxTemplate));
    }

    /**
     * 修改微信公众号的模板消息列
     */
    @SaCheckPermission("kitchen:template:edit")
    @Log(title = "微信公众号的模板消息列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxTemplate wxTemplate) {
        return toAjax(wxTemplateService.updateById(wxTemplate));
    }

    /**
     * 删除微信公众号的模板消息列
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:template:remove")
    @Log(title = "微信公众号的模板消息列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(wxTemplateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
